﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

using System.IO;
using Utility1;
using System.Drawing;

namespace Thv.admin.Pages
{
    public partial class Quanly_Quangcao : System.Web.UI.UserControl
    {
        const string UploadDirectory = "~/trangchu/images/Quangcao/";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindata();
                bindanhmuc();
            }
        }

        void bindata()
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            var list = from k in dx.QuangCaos
                       select k;
            grvData.DataSource = list;
            grvData.DataBind();
        }
        void bindanhmuc()
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            var list = from k in dx.ListItemQCs
                       select k;
            cbDanhsach.DataSource = list;
            cbDanhsach.ValueField = "ID";
            cbDanhsach.TextField = "ListItemName";
            cbDanhsach.DataBind();
            cbDanhsach.SelectedIndex = 0;
        }

        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            QuangCao qc = new QuangCao();
            qc.DanhMucID = 3;
            qc.DatePost = DateTime.Now;
            qc.flag = true;
            qc.giaChu = String.Format("{0:0,0}", double.Parse(txtGia.Value.ToString()));
            qc.giaMoi = double.Parse(txtGia.Value.ToString());
            if (FileUpload1.HasFile)
            {
                string file = SavePostedFile(FileUpload1);
                qc.images = file;
            }
            qc.linkImage = txtlink.Text;
            qc.Name = txtten.Text;
            qc.ListItemID = int.Parse(cbDanhsach.Value.ToString());
            qc.isLevel = int.Parse(cbLoai.Value.ToString());
            dx.QuangCaos.InsertOnSubmit(qc);
            dx.SubmitChanges();

            bindata();
        }




        string SavePostedFile(System.Web.UI.WebControls.FileUpload uploadedFile)
        {
            if (uploadedFile.FileName == "")
                return string.Empty;

            Random r = new Random(9999);
            int n = r.Next(999999);
            string ThumbnailFileName = n + FileUpload1.FileName;
            string fileName = Path.Combine(MapPath(UploadDirectory), ThumbnailFileName);
            using (Image original = Image.FromStream(uploadedFile.FileContent))
            using (Image thumbnail = PhotoUtils.Inscribe(original, 200))
            {
                PhotoUtils.SaveToJpeg(thumbnail, fileName);
            }
            return ThumbnailFileName;
        }

        protected void cbPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            switch (e.Parameter)
            {
                
                case "Xoa":
                    DataClasses1DataContext dx = new DataClasses1DataContext();
                    int id = (int)grvData.GetRowValues(grvData.FocusedRowIndex, "ID");
                    QuangCao p = new QuangCao();
                    p = dx.QuangCaos.Single(f => f.ID == id);
                    dx.QuangCaos.DeleteOnSubmit(p);
                    dx.SubmitChanges();
                    bindata();
                    break;
                case "Duyet":
                    DataClasses1DataContext dx1 = new DataClasses1DataContext();
                    int id1 = (int)grvData.GetRowValues(grvData.FocusedRowIndex, "ID");
                    var list = from k in dx1.QuangCaos
                               where k.ID == id1
                               select k;
                    bool kich = false;
                    foreach (QuangCao pr in list)
                        kich = (bool)pr.flag;
                    if (kich == true)
                        kich = false;
                    else
                        kich = true;
                    QuangCao p1 = new QuangCao();
                    p = dx1.QuangCaos.Single(f => f.ID == id1);
                    p.flag = kich;
                    dx1.SubmitChanges();
                    bindata();
              
                    break;
               
            }
        }
    }
}